//
//  AttachManagerService.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-27.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "AttachManagerService.h"

@implementation AttachManagerService

+(NSMutableArray *)getAllAttachFile
{
    NSMutableArray *allAttachFile = [[NSMutableArray alloc]init];
    NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/",NSHomeDirectory()];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileList = [[NSArray alloc] init];
    
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    fileList = [fileManager contentsOfDirectoryAtPath:savePath error:&error];
    
    for (int i=0; i<fileList.count; i++) {
        NSString *DirPath = [NSString stringWithFormat:@"%@/Documents/attaches/%@",NSHomeDirectory(),[fileList objectAtIndex:i]];
        NSArray *singleDirFilesList = [fileManager contentsOfDirectoryAtPath:DirPath error:&error];
        for (int j=0; j<singleDirFilesList.count; j++) {
            NSString *fileName = [singleDirFilesList objectAtIndex:j];
            NSString *singleFilePath = [NSString stringWithFormat:@"%@/%@",DirPath,fileName];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:fileName forKey:@"filename"];
            [dic setObject:singleFilePath forKey:@"filepath"];
            [allAttachFile addObject:dic];
            [dic release];
        }
    }
    return [allAttachFile autorelease];
}

+(void)deleteAllFile
{
    NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/",NSHomeDirectory()];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileList = [[NSArray alloc] init];
    
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    fileList = [fileManager contentsOfDirectoryAtPath:savePath error:&error];
    
    for (int i=0; i<fileList.count; i++) {
        NSString *DirPath = [NSString stringWithFormat:@"%@/Documents/attaches/%@",NSHomeDirectory(),[fileList objectAtIndex:i]];
        NSArray *singleDirFilesList = [fileManager contentsOfDirectoryAtPath:DirPath error:&error];
        for (int j=0; j<singleDirFilesList.count; j++) {
            NSString *fileName = [singleDirFilesList objectAtIndex:j];
            NSString *singleFilePath = [NSString stringWithFormat:@"%@/%@",DirPath,fileName];
            [[NSFileManager defaultManager] removeItemAtPath:singleFilePath error:&error];
        }
    }
}

@end
